
package com.sy.core.web.system;


import java.util.ArrayList;
import java.util.List;

import com.sy.core.model.common.Tree;
import com.sy.core.util.BaseUUID;
import com.sy.core.util.RestResult;
import com.sy.core.web.BaseCtl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.sy.core.model.SysDeptment;
import com.sy.core.service.SysDeptmentService;




/**
 * 	TODO 请用一句话描述该类的作用
 * @version 1.0
 * @since 1.0
 */


@Api
@ApiModel(value = "sysDeptmentController",description = "sysDeptment信息操作接口")
@RestController
@RequestMapping("sysDeptment")
public class SysDeptmentCtl extends BaseCtl {

    protected final Logger logger = LoggerFactory.getLogger(SysDeptmentCtl.class);
	
	
    @Autowired
    private SysDeptmentService  sysDeptmentService;
    

    
    /**
     * Action for Add
     */
    @ApiOperation(value = "新增信息")
    @PostMapping(value = "/save",produces = "application/json;charset=utf-8")
    public RestResult save(SysDeptment sysDeptment){
        RestResult rr = null;
        try {
            sysDeptment.setPk(BaseUUID.getUUID());
            sysDeptmentService.insert(sysDeptment);
            rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FAIL",e);
            rr=new RestResult("201","FAIL");
        }
        return rr;
       
    }

    /**
     * Action for update
     */
    @ApiOperation(value = "修改信息")
    @PostMapping(value = "/update",produces = "application/json;charset=utf-8")
    public RestResult update(SysDeptment sysDeptment){
         sysDeptmentService.update(sysDeptment);
        return new RestResult("200","SUCCESS");
    }
    
    /**
     * Action for SelectOneByKey
     */
    @ApiOperation(value = "根据主键查询信息",notes = "采用/后直接传参即可")
    @GetMapping(value = "/query/{key}",produces = "application/json;charset=utf-8")
    public RestResult queryByPrimaryKey(@PathVariable("key") String key){
       try{
            SysDeptment sysDeptment = sysDeptmentService.queryByPrimaryKey(key);
      	 	return new RestResult("200","SUCCESS",sysDeptment);
       }catch(Exception e){
    	   	logger.error("Query Model SysDeptment By PrimaryKey Is Fail:"+e.getMessage());
       }
       		return new RestResult("201","FAIL");
    }
    
    /**
     * Action for SelectList
     */
    @ApiOperation(value = "条件获取列表信息",notes = "不传参数默认查询所有")
    @GetMapping(value = "list",produces = "application/json;charset=utf-8")
    public RestResult queryList(SysDeptment sysDeptment){
        RestResult rr = null;
       try{
      		List<SysDeptment> sysDeptmentList = sysDeptmentService.queryByCondition(sysDeptment);
      	 	rr= new RestResult("200","SUCCESS",sysDeptmentList);
       }catch(Exception e){
    	   logger.error("Query Model SysDeptment List Fail :"+e.getMessage());
    	   rr=new RestResult("201","FAIL");
       }
        return  rr;
    }
    
    /**
     * Action for DeleteModel
     */
    @ApiOperation(value = "根据主键删除信息",notes = "key='主键'")
    @GetMapping("/delete/{parentPk}")
    public RestResult delete(@PathVariable("parentPk") String parentPk){
        RestResult rr = null;
        try {
            sysDeptmentService.delete(parentPk);
            rr = new RestResult("200","SUCCESS");
        } catch (Exception e) {
            logger.error("FAIL",e);
            rr = new RestResult("201","FAIL");
        }
        return rr;
    }

    /**
     * 通过父pk查询树的子节点
     */
    @ApiOperation(value = "通过父pk查询树的子节点",notes = "用来查询树的节点")
    @GetMapping(value = "treeList",produces = "application/json;charset=utf-8")
    public RestResult treeList(SysDeptment sysDeptment){
        RestResult rr = null;
        List<Tree> treeList = new ArrayList<>();
        try{
            List<SysDeptment> sysDeptmentList = sysDeptmentService.queryByCondition(sysDeptment);
            for (int i = 0; i <sysDeptmentList.size() ; i++) {
                Tree tree = new Tree();
                sysDeptment.setParentPk(sysDeptmentList.get(i).getPk());
                List<SysDeptment> sysDeptments = sysDeptmentService.queryByCondition(sysDeptment);
                if(sysDeptments.size()<1){
                    tree.setLeaf(true);
                }
                tree.setPk(sysDeptmentList.get(i).getPk());
                tree.setName(sysDeptmentList.get(i).getFullname());
                treeList.add(tree);
            }
            rr= new RestResult("200","SUCCESS",treeList);
        }catch(Exception e){
            logger.error("Query Model SysDeptment List Fail :"+e.getMessage());
            rr=new RestResult("201","FAIL");
        }
        return  rr;
    }
   
}
